package com.karson.front.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.karson.common.model.CirclesInfo;
import com.karson.common.model.TagInfo;
import com.karson.common.request.PageParamRequest;
import com.karson.common.response.CommonResult;
import com.karson.common.response.TagResponse;
import com.karson.service.service.TagInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ZKS
 */
@RestController
@RequestMapping("/api/tag")
@Tag(name = "标签管理接口")
public class TagController {
    @Autowired
    private TagInfoService tagInfoService;
    @Operation(summary = "根据专辑Id查找标签信息")
    @GetMapping("/getTagStat/{albumId}/{tagId}")
    public CommonResult<TagResponse> getTagStat(@PathVariable Integer albumId, @PathVariable Integer tagId) {
        if (albumId != null && albumId < 0 && tagId != null && tagId < 0) {
            return CommonResult.failed("获取失败");
        }
        TagResponse tagResponse = tagInfoService.getTagStat(albumId,tagId);
        return CommonResult.success(tagResponse);
    }
    @Operation(summary = "更新标签数据")
    @PostMapping("/update/{albumId}/{tagId}")
    public CommonResult<String> updateTagStat(@PathVariable Integer albumId, @PathVariable Integer tagId,@RequestParam String type) {
        if (albumId != null && albumId < 0 && tagId != null && tagId < 0) {
            return CommonResult.failed("获取失败");
        }
        tagInfoService.changeTagStat(albumId,tagId,type);
        return CommonResult.success();
    }
    @Operation(summary = "分页列表")
    @GetMapping("/list")
    public CommonResult<IPage<TagResponse>> getList(@ModelAttribute @ParameterObject PageParamRequest pageParamRequest) {
        IPage<TagResponse> tagInfoIPage = new Page<>(pageParamRequest.getCurrent(), pageParamRequest.getSize());
        IPage<TagResponse> tagInfo =  tagInfoService.pageList(tagInfoIPage);
        return CommonResult.success(tagInfo);
    }
    @Operation(summary = "用户自选标签")
    @PostMapping("add/{albumId}/{tagId}")
    public CommonResult<String> addTag(@PathVariable Integer albumId, @PathVariable Integer tagId) {
        if (albumId != null && albumId < 0 && tagId != null && tagId < 0) {
            return CommonResult.failed("获取失败");
        }
        Boolean success = tagInfoService.addTag(albumId,tagId);
        return success ? CommonResult.success() : CommonResult.failed();
    }
    @Operation(summary = "获取全部标签")
    @GetMapping("/all")
    public CommonResult<List<TagInfo>> getList(
            @RequestParam(value = "sortDesc", required = false, defaultValue = "true") boolean sortDesc
    ) {
        LambdaQueryWrapper<TagInfo> queryWrapper = Wrappers.lambdaQuery();
        if (sortDesc) {
            queryWrapper.orderByDesc(TagInfo::getNum);
        } else {
            queryWrapper.orderByAsc(TagInfo::getNum);
        }

        List<TagInfo> list = tagInfoService.list(queryWrapper);
        return CommonResult.success(list);
    }
}
